package com.vast.ui.component.socketinterface.server;

import com.vast.ui.component.socketinterface.listner.IReceiveListener;
import com.vast.ui.component.socketinterface.listner.ISocketConnectionListener;
import com.vast.ui.component.socketinterface.request.RequestQueue;
import com.vast.ui.component.state.Sequence;


public class ServerService implements IServer {
	public  int                  serverRecvPort;
	public  int                  serverSendPort;
	private final Sequence       sequence;
	private final SocketService  receiveService;
	private final SocketService  sendService;
	private final DecodeRequester decodeRequester;
	private final RequestQueue   receiveQueue;
	
	public ServerService(int serverPort, Sequence sequence, RequestQueue sendQueue) {
		this.serverRecvPort   = serverPort;
		this.serverSendPort   = serverPort + 1;
		this.sequence         = sequence;
		this.receiveQueue     = new RequestQueue();
		this.decodeRequester  = new DecodeRequester(sequence, receiveQueue);
		this.receiveService   = new ReceiveService(sequence, receiveQueue, decodeRequester);
		this.sendService      = new SendService(sequence, sendQueue);
	}
	
	public void openConnection() {
		if(!sequence.isReceiveConnection()) {
			receiveService.startup(serverRecvPort);
			//decodeRequester.startup();
			sequence.openReceiveConnection();
		}
		if(!sequence.isSendConnection()) {
			//sendService.startup(serverSendPort);
			sequence.openSendConnection();
		}
	}

	public void closeConnection() {
		// TODO Auto-generated method stub
		sequence.closeSendConnection();
		sequence.closeReceiveConnection();
		//sendService.shutdown();
		receiveService.shutdown();
	}

	public void addReceiveListener(IReceiveListener listener) {
		// TODO Auto-generated method stub
		decodeRequester.addReceiveListener(listener);
	}

	public void addSocketConnectionListener(ISocketConnectionListener listener) {
		// TODO Auto-generated method stub
		decodeRequester.addSocketConnectionListener(listener);
	}

	public void removeReceiveListener(IReceiveListener listener) {
		// TODO Auto-generated method stub
		decodeRequester.removeReceiveListener(listener);
	}

	public void removeSocketConnectionListener(
			ISocketConnectionListener listener) {
		// TODO Auto-generated method stub
		decodeRequester.removeSocketConnectionListener(listener);
	}
}
